home *** CD-ROM | disk | FTP | other *** search
/ Enter 2010 January / ENTER_2010_01.iso / Programy / Gry / Base_Invaders_ / BaseInvadersSetup1.3.exe / {app} / Scripts / SpawnSystem.lua < prev    next >
Encoding:
Text File  |  2007-01-25  |  9.1 KB  |  313 lines

  1.  
  2. -- This file describes the new Spawn System (Matt Miner 3-27-06) 
  3.  
  4. ---------------------------------------------------------------------------
  5. --                        SPAWNING LOCATION CODE 
  6. --        These are default functions that can be used to specify
  7. --        where invaders spawn from. Feel free to add more. NOTE:
  8. --        The function must take no parameters and return a Vector3
  9. ---------------------------------------------------------------------------
  10.  
  11. -- Randomly positioned in a circle around the tower
  12. function CircleAroundTower()
  13.     local centerPos = Vector3( 256, 0, 256 );
  14.     
  15.     local tower = G.GetCogName("Tower")
  16.     -- Check that the tower still exists, and get it's center:
  17.     if( tower ~= nil and tower.IsValid() ) then
  18.         centerPos = tower.GetPosition()    
  19.     end
  20.  
  21.     -- Get a value between 0 and 2*PI
  22.     local randpos = math.random() * 2.0 * math.pi;                
  23.     return Vector3( centerPos.x + math.cos(randpos) * 128, centerPos.y,  centerPos.z + math.sin(randpos) * 128  );
  24. end
  25.  
  26. -- Randomly positioned in a circle around the tower
  27. function SmallCircleAroundTower()
  28.     local centerPos = Vector3( 256, 0, 256 );
  29.     
  30.     local tower = G.GetCogName("Tower")
  31.     -- Check that the tower still exists, and get it's center:
  32.     if( tower ~= nil and tower.IsValid() ) then
  33.         centerPos = tower.GetPosition()    
  34.     end
  35.  
  36.     -- Get a value between 0 and 2*PI
  37.     local randpos = math.random() * 2.0 * math.pi;                
  38.     return Vector3( centerPos.x + math.cos(randpos) * 64, centerPos.y,  centerPos.z + math.sin(randpos) * 64  );
  39. end
  40.  
  41. -- Anywhere valid on the map    --changed by STEFAN to stay closer to center (assuming 512 x 512 grid) NOTE: is only used when close to right side of map
  42. function RandomAnywhere()
  43.     local centerPos = Vector3( 256, 0, 256 );
  44.     
  45.     local tower = G.GetCogName("Tower")
  46.     -- Check that the tower still exists, and get it's center:
  47.     if( tower ~= nil and tower.IsValid() ) then
  48.         centerPos = tower.GetPosition()    
  49.     end
  50.  
  51.     return     centerPos + Vector3( math.random( 180 ) - 90, 0, math.random( 180 ) - 90 );
  52. end
  53.  
  54. SpawnPoints = {}
  55.  
  56. -- Uses "SpawnPoints" list of positions to use:
  57. function HardCodedSpawnPoints()
  58.  
  59.     local spawnPos = Vector3(0,0,0)
  60.     
  61.     if SpawnPoints ~= nil then
  62.         -- Choose a random (valid) value 
  63.         local maxSpawnNum = SpawnPoints.NumPoints    
  64.         local curSpawnPoint = math.random( maxSpawnNum )
  65.         
  66.         -- If the random index did not exist, get the next valid one        
  67.         if ( SpawnPoints[ curSpawnPoint ] == nil ) then
  68.             curSpawnPoint = next( SpawnPoints, curSpawnPoint )
  69.             
  70.             -- if the spawn point is still invalid (moved outside of list) get the first value:
  71.             if(  curSpawnPoint == nil ) then
  72.                 curSpawnPoint = next( SpawnPoints )
  73.             end
  74.             
  75.         end
  76.  
  77.         if SpawnPoints[ curSpawnPoint ] ~= nil then
  78.             spawnPos = SpawnPoints[ curSpawnPoint ]
  79.         end
  80.     end
  81.     
  82.     return spawnPos
  83.     
  84. end
  85.  
  86. ---------------------------------------------------------------------------
  87. --                        SPAWN WAVE STRUCTURE
  88. --        This is the definition for the spawn wave structure used
  89. --        by the spawn system. The structure contains stats, spawning 
  90. --        probabilities, and Several Spawning functions.
  91. ---------------------------------------------------------------------------
  92.  
  93. -- Manually updates the probability totals
  94. function InitUpdateTotalProbabilty( self )
  95.  
  96.     self.ProbTotal = 0;
  97.     for invadername,prob in pairs( self.Prob ) do    
  98.         self.ProbTotal = self.ProbTotal + prob;
  99.     end
  100. end
  101.  
  102. -- Increments time, and checks if a spawn should be done now:
  103. function InitShouldSpawn( self )
  104.       
  105.       -- Update the time
  106.       self.SpawnTime = self.SpawnTime + self.SpawnSpeed * GameTimeDiff
  107.  
  108.     if( self.TotalSpawn ~= nil ) then
  109.          if( self.TotalSpawn <= 0 ) then
  110.             return false;
  111.          end
  112.     end
  113.  
  114.     if( self.SpawnTime > self.NextGroupSize ) then
  115.         
  116.         -- Prevent overflow (Affected by choppy framerates)
  117.         self.SpawnTime = self.NextGroupSize
  118.         
  119.         -- Check what the restriction on # of invaders is
  120.         return ( self.MaxSpawn >= NumInvaders + self.NextGroupSize )
  121.  
  122.     end
  123.     
  124.     return false
  125. end
  126.  
  127. randomOffsets = {}
  128. randomOffsets[1] = Vector3( 0, 0, 0 )
  129. randomOffsets[2] = Vector3( 3, 0.5, 0 )
  130. randomOffsets[3] = Vector3( 0, 0.25, 3 )
  131. randomOffsets[4] = Vector3( 3, 0, 3 )
  132. randomOffsets[5] = Vector3(-3, -0.5, 0 )
  133. randomOffsets[6] = Vector3( 0, 0,-3 )
  134. randomOffsets[7] = Vector3(-3, 0.25,-3 )
  135. randomOffsets[8] = Vector3(-6, -0.25, 6 )
  136. randomOffsets[9] = Vector3( 6, 0.5,-6 )
  137. randomOffsets[10] = Vector3( 6, 0, 6 )
  138.  
  139. -- Knowing that a spawn is valid (May be called by level) spawn it
  140. function InitDoSpawn( self )
  141.     
  142.     local spawnPos = self.WhereToSpawn()
  143.     
  144.     self:UpdateTotalProbabilty( )
  145.     
  146.     
  147.     
  148.     for curInvNum = 1, self.NextGroupSize do
  149.         -- Find What type to spawn
  150.         local rand = math.random();        
  151.         rand = rand * self.ProbTotal;
  152.         
  153.         local total;
  154.         total = 0.0;
  155.         for invadername,prob in pairs( self.Prob ) do    
  156.             total = total + prob;
  157.  
  158.             -- Spawn it
  159.             if( total > rand ) then
  160.                 local invader = G.Allocate( invadername );                
  161.         
  162.                 -- Apply a small random offset from the given position to lessen stacking with groups:
  163.                 offset = randomOffsets[curInvNum] -- Vector3(( math.random() - 0.5 )* 2.0, 0, ( math.random() - 0.5 )* 2.0 ) 
  164.                 
  165.                 invader.SetPosition( spawnPos + offset ); 
  166.                 
  167.                 -- Have the invader move away from the position:
  168.                 invader.SetVelocity( offset );                                 
  169.                 
  170.                 invader.Init();
  171.                 
  172.                 self.SpawnTime = self.SpawnTime - 1;
  173.                 
  174.                 -- 
  175.                 if( self.TotalSpawn ~= nil ) then
  176.                     self.TotalSpawn = self.TotalSpawn - 1
  177.                     if( self.TotalSpawn == 0 and self.FinishCallback ~= nil ) then
  178.                         self.FinishCallback()
  179.                     end
  180.                 end
  181.                 
  182.                 break;
  183.             end
  184.         end            
  185.     end
  186.     
  187.     -- Update for the next spawn:
  188.     if math.random() < self.GroupPercent then
  189.         self.NextGroupSize = self.GroupSize
  190.         if( self.TotalSpawn ~= nil and self.NextGroupSize > self.TotalSpawn ) then
  191.             self.NextGroupSize  = self.TotalSpawn;
  192.         end
  193.     else
  194.         self.NextGroupSize = 1
  195.     end
  196.         
  197.     
  198. end
  199.  
  200. -- This creates the structure used for the spawning system.
  201. function createSpawnStruct( )
  202.     
  203.     local spawnStruct = {}
  204.  
  205.     -- SPAWN STATS --
  206.     spawnStruct.SpawnSpeed        = 0.0;
  207.     spawnStruct.MaxSpawn        = 0;        -- Simultaneous
  208.     spawnStruct.TotalSpawn        = nil;        -- Ever (nil if infinite)
  209.     spawnStruct.GroupPercent    = 0.0;
  210.     spawnStruct.GroupSize        = 1;
  211.     spawnStruct.WhereToSpawn    = RandomAnywhere
  212.     spawnStruct.FinishCallback    = nil            -- Called when the wave finished spawning
  213.  
  214.     -- INVADER SPAWN PERCENTS: --
  215.     spawnStruct.Prob = {};
  216.     spawnStruct.Prob.Miner        = 0.0;
  217.     spawnStruct.Prob.Squad        = 0.0;
  218.     spawnStruct.Prob.Bomber        = 0.0;
  219.     spawnStruct.Prob.Kamikaze    = 0.0;
  220.     spawnStruct.Prob.Driller    = 0.0;
  221.     spawnStruct.Prob.Psychic    = 0.0;
  222.     spawnStruct.Prob.Ninja        = 0.0;
  223.     spawnStruct.Prob.Spiker        = 0.0;
  224.     spawnStruct.Prob.Stacker    = 0.0;
  225.     spawnStruct.Prob.Basic        = 0.0;
  226.     spawnStruct.ProbTotal        = 0.0;    -- Total probabilty for all groups, updated manually.
  227.  
  228.     -- INTERNAL VARIABLES --
  229.     spawnStruct.SpawnTime        = 0.0;    -- Timing variable used to determine if enough time has passed to spawn (>1.0)
  230.     spawnStruct.NextGroupSize    = 1;
  231.  
  232.     spawnStruct.UpdateTotalProbabilty = InitUpdateTotalProbabilty
  233.     spawnStruct.ShouldSpawn     = InitShouldSpawn;
  234.     spawnStruct.DoSpawn        = InitDoSpawn;
  235.  
  236.     return spawnStruct;
  237. end
  238.  
  239. ------------------- EXAMPLE WAVES ---------------------
  240. DefaultWaveStruct = createSpawnStruct()
  241.  
  242. ------------------- EXAMPLE WAVES ---------------------
  243. BasicWave = createSpawnStruct()
  244.  
  245. BasicWave.Prob.Basic    = 1.0;
  246.  
  247. BasicWave.SpawnSpeed    = 0.5;
  248. BasicWave.MaxSpawn        = 25;
  249. BasicWave.GroupPercent    = 0.1;
  250. BasicWave.GroupSize        = 3;
  251. BasicWave.WhereToSpawn    = CircleAroundTower
  252.  
  253. ------------------- EXAMPLE WAVES ---------------------
  254. ExposiveWave = createSpawnStruct()
  255.  
  256. ExposiveWave.Prob.Kamikaze    = 0.5;
  257. ExposiveWave.Prob.Bomber    = 1.0;
  258.  
  259. ExposiveWave.SpawnSpeed        = 0.5;
  260. ExposiveWave.MaxSpawn        = 25;
  261. ExposiveWave.GroupPercent    = 0.1;
  262. ExposiveWave.GroupSize        = 3;
  263. ExposiveWave.WhereToSpawn    = CircleAroundTower
  264.  
  265. ---------------------------------------------------------------------------
  266. --                            SPAWNING CODE 
  267. --        This is the code used to set up the spawning of invaders. 
  268. --        To use: 1. Create a new spawn wave using the createSpawnStruct() function.
  269. --    2. Call AddWave() and pass the new wave in, allong with an ID. (String or integer)
  270. --    3. Call StopWave() and pass the ID of the wave to be stopped.
  271. ---------------------------------------------------------------------------
  272.  
  273. SpawnWaves = {}
  274.  
  275. -- Adds a new wave to the given ID
  276. function AddWave( waveID, waveStruct )
  277.     waveStruct.SpawnTime = 0
  278.     
  279.     if( SpawnWaves[waveID] == nil ) then
  280.         SpawnWaves[waveID] = waveStruct
  281.     else
  282.         -- The spawn wave already existed here.
  283.         -- For now, this will override the existing wave.
  284.         SpawnWaves[waveID] = waveStruct     
  285.     end
  286. end
  287.  
  288. -- Stop the given wave number
  289. function StopWave( waveID )
  290.     if( SpawnWaves[waveID] ~= nil ) then
  291.         SpawnWaves[waveID] = nil
  292.     end
  293. end
  294.  
  295. -- End (erase) all waves
  296. function EndAllWaves()
  297.     SpawnWaves = nil    
  298.     SpawnWaves = {}
  299. end
  300.  
  301.  
  302. -- Update all SpawnWaves
  303. function SpawnSystemUpdate()
  304.  
  305.     for waveID, waveStruct in pairs( SpawnWaves ) do
  306.         if( waveStruct:ShouldSpawn( ) ) then
  307.             waveStruct:DoSpawn( )
  308.         end
  309.     end
  310. end
  311.  
  312. -- Gmain: Automatic Updated Function List
  313. GMain[ "SpawnSystemUpdate" ] = SpawnSystemUpdate;